#include #include #include #include #include using std::ifstream; using std::ofstream; using std::ios_base; using std::string; using std::cout; using std::cin; using std::endl; struct BitmapFileHeader { char header[2]; unsigned int fileSize; unsigned short reserved1; unsigned short reserved2; unsigned int offset; BitmapFileHeader() { header[0] = 'B'; header[1] = 'M'; fileSize = 1152054; offset = 54; } }; struct BitmapImageHeader { unsigned int headerSize; int width; int height; short paneNum; short bitspp; int compression; int size; int hresolution; int vresolution; int numColors; int numImpColors; BitmapImageHeader() { headerSize = 40;// 4 Header Size - Must be at least 40 width = 800;// 4 Image width in pixels height = 480 ;// 4 Image height in pixels paneNum = 1;// 2 Must be 1 bitspp = 24;// 2 Bits per pixel - 1, 2, 4, 8, 16, 24, or 32 compression = 0;// 4 Compression type (0 = uncompressed) size = 1152000;// 4 Image Size - may be zero for uncompressed images hresolution = 0;// 4 Preferred resolution in pixels per meter vresolution = 0;// 4 Preferred resolution in pixels per meter numColors = 0;// 4 Number Color Map entries that are actually used numImpColors = 0;// 4 Number of significant colors } }; struct Pixel { unsigned char blue; unsigned char green; unsigned char red; }; struct Image { BitmapFileHeader bitmapFileHeader; BitmapImageHeader bitmapInfoHeader; Pixel* pixels; void setBackground(unsigned char blue,unsigned char green,unsigned char red) { int i = 0; for(int row =0; row < bitmapInfoHeader.height; row++) { for(int column = 0; column < bitmapInfoHeader.width;column++) { //cout << abs(sin(column)) *bitmapInfoHeader.width << endl; //if((int)(abs(sin(column)) *bitmapInfoHeader.width) == row) if(((column/10) %2 == 0 && (row/10) %2 == 0)|| (column/10) %2 == 1 && (row/10) %2 == 1) { pixels[i].red = 255; pixels[i].blue = 0; pixels[i].green = 0; } else { pixels[i].red = 0; pixels[i].blue = 0; pixels[i].green = 0; } i++; } } } }; void main() { srand(time(NULL)); Image image; image.pixels = new Pixel[image.bitmapInfoHeader.width * image.bitmapInfoHeader.height]; //Dynamically allocate memory for the pixels image.setBackground(255,0,0); //int garbageByteCount = (4 - ((image.bitmapInfoHeader.width * 3)%4))%4; int garbageByteCount = image.bitmapInfoHeader.width % 4; unsigned char garbage[3] = {0,0,0}; int pixelIndex = 0; for(int row = 0; row < image.bitmapInfoHeader.height; row++) { for(int column = 0; column < image.bitmapInfoHeader.width; column++) { if(image.pixels[pixelIndex].red == 255 && image.pixels[pixelIndex].blue == 255 && image.pixels[pixelIndex].green == 255) { image.pixels[pixelIndex].red = 0; image.pixels[pixelIndex].blue = 0; image.pixels[pixelIndex].green = 0; } pixelIndex++; //cout << (int)blue << ":" << (int)green << ":" << (int)red << endl; } } ofstream fout("out.bmp", ios_base::binary); fout.write(image.bitmapFileHeader.header, 2); fout.write((char*)&image.bitmapFileHeader.fileSize, 4); fout.write((char*)&image.bitmapFileHeader.reserved1, 2); fout.write((char*)&image.bitmapFileHeader.reserved2, 2); fout.write((char*)&image.bitmapFileHeader.offset, 4); fout.write((char*)&image.bitmapInfoHeader.headerSize, 4); fout.write((char*)&image.bitmapInfoHeader.width, 4); fout.write((char*)&image.bitmapInfoHeader.height, 4); fout.write((char*)&image.bitmapInfoHeader.paneNum, 2); fout.write((char*)&image.bitmapInfoHeader.bitspp, 2); fout.write((char*)&image.bitmapInfoHeader.compression, 4); fout.write((char*)&image.bitmapInfoHeader.size, 4); fout.write((char*)&image.bitmapInfoHeader.hresolution, 4); fout.write((char*)&image.bitmapInfoHeader.vresolution, 4); fout.write((char*)&image.bitmapInfoHeader.numColors, 4); fout.write((char*)&image.bitmapInfoHeader.numImpColors, 4); pixelIndex = 0; for(int row = 0; row < image.bitmapInfoHeader.height; row++) { for(int column = 0; column < image.bitmapInfoHeader.width; column++) { fout.write((char*)&image.pixels[pixelIndex].blue, 1); fout.write((char*)&image.pixels[pixelIndex].green, 1); fout.write((char*)&image.pixels[pixelIndex].red, 1); pixelIndex++; //cout << (int)blue << ":" << (int)green << ":" << (int)red << endl; } fout.write((char*)garbage, garbageByteCount); } //fin.close(); //cout << image; delete[] image.pixels; //Dont do this until you write fout.close(); }